package com.dzh.dzhhub.shiro;

import com.dzh.dzhhub.entity.Permission;
import com.dzh.dzhhub.entity.User;
import com.dzh.dzhhub.service.LoginService;
import com.dzh.dzhhub.util.JedisUtil;
import com.dzh.dzhhub.util.JwtUtils;
import com.dzh.dzhhub.util.constants.Constants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Collection;

/**
 * @author DZHooo
 * @version 1.0
 * @date 2021/11/16 9:45
 * @description: TODO
 */


public class UserRealm extends AuthorizingRealm {

//    /**
//     * RefreshToken过期时间
//     */
//    @Value("${refreshTokenExpireTime}")
//    private String refreshTokenExpireTime;

    @Autowired
    private LoginService loginService;

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Session session = SecurityUtils.getSubject().getSession();
        Permission permission = (Permission)session.getAttribute(Constants.SESSION_USER_PERMISSION);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addStringPermissions( permission.getPermissionList());
        return authorizationInfo;
    }

    //验证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String)authenticationToken.getPrincipal();
        String password = new String((char[])authenticationToken.getCredentials());
        User user = loginService.getUser(username,password);
        if(user == null) {
            throw new UnknownAccountException();
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user,user.getPassword(),getName());
        user.setPassword("");
        SecurityUtils.getSubject().getSession().setAttribute(Constants.SESSION_USER_INFO,user);

//        // 清除可能存在的Shiro权限信息缓存
//        if(JedisUtil.exists(Constants.PREFIX_SHIRO_CACHE + username)) {
//            JedisUtil.delKey(Constants.PREFIX_SHIRO_CACHE + username);
//        }
//        // 设置RefreshToken，时间戳为当前时间戳，直接设置即可(不用先删后设，会覆盖已有的RefreshToken)
//        String currentTimeMillis = String.valueOf(System.currentTimeMillis());
//        JedisUtil.setObject(Constants.PREFIX_SHIRO_REFRESH_TOKEN + username,currentTimeMillis,Integer.parseInt(refreshTokenExpireTime));
//        // 从Header中Authorization返回AccessToken，时间戳为当前时间戳
//        String token = JwtUtils.sign(username,currentTimeMillis);



        return authenticationInfo;
    }
}
